<?php

namespace app\api\controller;
//require_once ROOT_PATH . '/vendor/openapi-sdk-php-client-master/autoload.php';
use app\common\controller\Api;
/*use AlibabaCloud\SDK\Dypnsapi\V20170525\Dypnsapi;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dypnsapi\V20170525\Models\CreateVerifySchemeRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;*/

/**
 * 百度AI接口
 */
class Speech extends Api
{

    public function _initialize()
    {error_reporting(0);


    }

    /**
     *鉴权令牌获取AccessToken
     */
    public function gettoken()
    {
        $getres = curltopost('https://aip.baidubce.com/oauth/2.0/token',['client_id'=>'sQYvT6oNxG1L2MUBiCP8sk2f','client_secret'=>'mWGNWyoLDesaVp5HwvMKgeecYATzEdKN','grant_type'=>'client_credentials']);
        if($getres['code']==200){
            $this->success($getres['data']);
        }else{
            $this->error($getres['data']['msg']);
        }
    }

    /**
     *词库
     */
    public function thesaurus()
    {
        $speechword = input('speechword');
        $getres = "";
        $spearray = \app\common\model\Speech::select();
        if(\app\common\model\Speech::where('name', "LIKE", '%'.$speechword.'%')->find()){
            $getres = \app\common\model\Speech::where('name', "LIKE", '%'.$speechword.'%')->find();
        }else{
            foreach($spearray as $val){
                foreach(explode(',',$val['name']) as $value){
                    if(strpos($speechword,$value)!==false){
                        $getres = \app\common\model\Speech::where('name', "LIKE", '%'.$value.'%')->find();
                        break;
                    }
                }
            }
        }
        //$getres = \app\common\model\Speech::where('name', "LIKE", '%'.$speechword.'%')->find();
        if($getres){
            if(input('longitude')) \app\common\model\UserSpeech::create(['province'=>input('province'),'city'=>input('city'),'region'=>input('region'),'address'=>input('address'),'system'=>input('system'),'longitude'=>input('longitude'),'latitude'=>input('latitude')]);
            $lon = input('longitude');
            $lat = input('latitude');
            $ciy = input('city');
            if($getres['answer'] == '0571-88293900'){
                if($lat && $lon) $maplist = \think\Db::query("SELECT id,address,mobile , (6371 * 2 * ASIN(SQRT( POWER(SIN(( {$lat} - latitude) *  pi()/180 / 2), 2) +COS( {$lat} * pi()/180) * COS(latitude * pi()/180) * POWER(SIN(( {$lon} - longitude) * pi()/180 / 2), 2) ))) as distance  FROM `yz_speech_shop` HAVING  distance <= 10 ORDER BY distance;");
                $getres['answer'] = str_replace(')','-',str_replace('(','',$maplist[/*mt_rand(0,count($maplist))*/0]['mobile'])) ?: ($ciy?\app\common\model\SpeechShop::where('address','LIKE','%'.$ciy.'%')->find()['mobile']:/*$getres['answer']*/'19988999998');
                //$getres['m'] = $maplist;
            }
            if($getres['answer'] == '0574-58992517'){
                if($lat && $lon) $maplist = \think\Db::query("SELECT id,address,mobile , (6371 * 2 * ASIN(SQRT( POWER(SIN(( {$lat} - latitude) *  pi()/180 / 2), 2) +COS( {$lat} * pi()/180) * COS(latitude * pi()/180) * POWER(SIN(( {$lon} - longitude) * pi()/180 / 2), 2) ))) as distance  FROM `yz_speech_shomedic` HAVING  distance <= 10 ORDER BY distance;");
                $getres['answer'] = str_replace(')','-',str_replace('(','',$maplist[/*mt_rand(0,count($maplist))*/0]['mobile'])) ?: ($ciy?\app\common\model\SpeechShomedic::where('address','LIKE','%'.$ciy.'%')->find()['mobile']:/*$getres['answer']*/'19988999998');
                //$getres['m'] = $maplist;
            }
            $this->success('成功',$getres);
        }else{
            $this->error('无相关回答');
        }
    }

    /**
     *版本
     */
    public function versions()
    {
        $getres = \app\admin\model\wanlshop\Version::order('id', "DESC")->find();
        if($getres){
            $this->success('成功',$getres);
        }else{
            $this->error('无更新');
        }
    }

    
    /*public function ttes(){
        $config = new Config([
            // 您的AccessKey ID
            "accessKeyId" => "LTAI4GF3hcdEMSfhu3QdFgyy",
            // 您的AccessKey Secret
            "accessKeySecret" => "VzI9x6XY15my1GQ7FLv94WxTZCgDbh"
        ]);
// 访问的域名
        $config->endpoint = "ecs-cn-hangzhou.aliyuncs.com";
        $client = new Dypnsapi($config);
        $runtime                 = new RuntimeOptions();
        $runtime->maxIdleConns   = 3;
        $runtime->connectTimeout = 10000;
        $runtime->readTimeout    = 10000;
        $request = new CreateVerifySchemeRequest();
        // 该参数值为假设值，请您根据实际情况进行填写
        $request->SchemeName = "合念watch";

// 该参数值为假设值，请您根据实际情况进行填写
        $request->AppName = "FS时光会";
        try {
            $response = $client->createVerifyScheme($request, $runtime);
            var_dump($response->Body->requestId);
        }catch (TeaUnableRetryError $e) {
            // 获取报错数据
            var_dump($e->getErrorInfo());
            // 获取报错信息
            var_dump($e->getMessage());
            // 获取最后一次报错的 Exception 实例
            var_dump($e->getLastException());
            // 获取最后一次请求的 Request 实例
            var_dump($e->getLastRequest());
        }
    }
    public function hhh()
    {
        $accessKeyId = "LTAI4GF3hcdEMSfhu3QdFgyy";
        $accessKeySecret = "VzI9x6XY15my1GQ7FLv94WxTZCgDbh";
        AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)
            ->asDefaultClient();

        try {
            $result = AlibabaCloud::rpc()
                ->product('Dypnsapi')
                ->scheme('https') // https | http
                ->version('2017-05-25')
                ->action('GetMobile')
                ->method('POST')
                ->host('dypnsapi.aliyuncs.com')
                ->options([
                    'query' => [
                        'RegionId' => "cn-hangzhou",
                        'AccessToken' => "Dfafdafad54229898"
                    ],
                ])
                ->request();

            var_dump( array('status' => 'success', 'data' => $result->toArray()));
        } catch (ClientException $e) {
            var_dump( array('status' => 'failed', 'code' => $e->getErrorCode(), 'msg' => $e->getErrorMessage()));
        } catch (ServerException $e) {
            var_dump( array('status' => 'failed', 'code' => $e->getErrorCode(), 'msg' => $e->getErrorMessage()));
        }
    }*/


}
